package com.hypo.business.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hypo.business.domain.Result;
import com.hypo.business.domain.dto.PageResultDto;
import com.hypo.business.domain.po.Pcd;
import com.hypo.business.domain.po.Supplier;
import com.hypo.business.domain.vo.SupplierVo;
import com.hypo.business.service.PcdService;
import com.hypo.business.service.SupplierService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

/**
 * @author hypo
 */
@RestController
@RequestMapping("/supplier")
@RequiredArgsConstructor
public class SupplierController {
    private final SupplierService supplierService;

    private final PcdService pcdService;


    /**
     * 分页条件查询
     */
    @GetMapping
    public Result pageQuery(SupplierVo vo) {
        IPage<Supplier> supplierPage = new Page<>(vo.getPage(), vo.getPageSize());
        LambdaQueryWrapper<Supplier> queryWrapper = new LambdaQueryWrapper<>();
        // 判断是否为空或空字符串
        boolean hasSupplierName = !Objects.isNull(vo.getSupplierName()) && !vo.getSupplierName().isEmpty();
        boolean hasLinkPhone = !Objects.isNull(vo.getLinkPhone()) && !vo.getLinkPhone().isEmpty();
        queryWrapper.like(hasSupplierName, Supplier::getSupplierName, vo.getSupplierName())
                .eq(hasLinkPhone, Supplier::getLinkPhone, vo.getLinkPhone());
        IPage<Supplier> pageInfo = supplierService.page(supplierPage, queryWrapper);
        List<Supplier> records = pageInfo.getRecords();
        long total = pageInfo.getTotal();

        if (!records.isEmpty()) {
            for (Supplier supplier : records) {
                Pcd cityPcd = pcdService.getById(supplier.getCityCode());
                Pcd provincePcd = pcdService.getById(supplier.getProvinceId());
                supplier.setCityName(cityPcd != null ? cityPcd.getName() : "");
                supplier.setProvinceName(provincePcd != null ? provincePcd.getName() : "");
            }
        }
        return Result.success(new PageResultDto<>(total, records));
    }

    /**
     * 新增渠道商
     */
    @PostMapping
    public Result add(@RequestBody SupplierVo vo) {
        Pcd pcd = pcdService.getById(vo.getPcdId());
        Supplier supplier = new Supplier();
        BeanUtils.copyProperties(vo, supplier);
        if (pcd != null && Objects.nonNull((pcd.getParentId()))) {
            supplier.setCityCode(pcd.getParentId());
            Integer parentId = pcd.getParentId();
            LambdaQueryWrapper<Pcd> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(Pcd::getId, parentId)
                    .orderByAsc(Pcd::getId).last("LIMIT 1");
            Pcd one = pcdService.getOne(queryWrapper, false);
            if (one != null && Objects.nonNull((one.getParentId()))) {
                supplier.setProvinceId(one.getParentId());
            }
        }
        supplier.setCreateTime(LocalDateTime.now());
        return Result.success(supplierService.save(supplier));
    }

    /**
     * 修改渠道商
     */
    @PutMapping
    public Result edit(@RequestBody Supplier supplier) {
        return Result.success(supplierService.updateById(supplier));
    }

    /**
     * 删除渠道商
     */
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable("id") Integer id) {
        return Result.success(supplierService.removeById(id));
    }
}
